package cn.iocoder.yudao.module.security.service.devicelog;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.security.controller.admin.devicelog.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.devicelog.SecurityDeviceLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.security.dal.mysql.devicelog.SecurityDeviceLogMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.security.enums.ErrorCodeConstants.*;

/**
 * 终端日志 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class SecurityDeviceLogServiceImpl implements SecurityDeviceLogService {

    @Resource
    private SecurityDeviceLogMapper deviceLogMapper;

    @Override
    public Long createDeviceLog(SecurityDeviceLogSaveReqVO createReqVO) {
        // 插入
        SecurityDeviceLogDO deviceLog = BeanUtils.toBean(createReqVO, SecurityDeviceLogDO.class);
        deviceLogMapper.insert(deviceLog);
        // 返回
        return deviceLog.getId();
    }

    @Override
    public void updateDeviceLog(SecurityDeviceLogSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceLogExists(updateReqVO.getId());
        // 更新
        SecurityDeviceLogDO updateObj = BeanUtils.toBean(updateReqVO, SecurityDeviceLogDO.class);
        deviceLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceLog(Long id) {
        // 校验存在
        validateDeviceLogExists(id);
        // 删除
        deviceLogMapper.deleteById(id);
    }

    private void validateDeviceLogExists(Long id) {
        if (deviceLogMapper.selectById(id) == null) {
            throw exception(DEVICE_LOG_NOT_EXISTS);
        }
    }

    @Override
    public SecurityDeviceLogDO getDeviceLog(Long id) {
        return deviceLogMapper.selectById(id);
    }

    @Override
    public PageResult<SecurityDeviceLogDO> getDeviceLogPage(SecurityDeviceLogPageReqVO pageReqVO) {
        return deviceLogMapper.selectPage(pageReqVO);
    }

}